<?php
class archives extends Front {

    public function index( $c ) {

        if ( $id = intval( R( 'id' ) ) ) {
            $where = array( 'id' => $id );
        } elseif ( $alias = R( 'alias' ) ) {
            $where = array( 'alias' => $alias );
        }

        if ( !$where ) {
            $this->show_404();
        }

        $this->model->table = 'article';
        if ( !$result = $this->model->find( $where ) ) {
            $this->show_404();
        }

        // 文章标签
        $tag = $this->getTag( $result[ 'id' ] );

       // print_r( $tag ); exit;

        if ( !$result[ 'content' ] ) {
            $result[ 'content' ] = "没有内容。";
            $description = $result[ 'title' ];
        } else {
            $result[ 'content' ] = htmlspecialchars_decode( $result[ 'content' ] );
            //$result[ 'content' ] = str_replace( '<!--more-->', '', $result[ 'content' ] );
            $description = htmlText( $result[ 'content' ], 80, true );
        }

        $keywords = $tag ? implode( ",", $tag ) : $this->setting[ 'webkey' ];
        $title = $result[ 'title' ] . $this->sort[ $result[ 'sid' ] ][ 'title' ];

        $this->model->incrField( array( 'id' => $result[ 'id' ] ), 'views' );

        include $this->display( $c );
    }

    public function getcode() {
        $this->Image()->Verify( 110, 30 );
    }

    public function get_back( $id ) {
        $this->model->table = 'article';
        $back = $this->model->find( "id > {$id}", 'id asc' );
        return $back ? "<a href=\"{$this->archives_url($back)}\" title=\"{$back['title']}\">{$back['title']}</a>" : '没有了。';
    }

    public function get_next( $id ) {
        $this->model->table = 'article';
        $next = $this->model->find( "id < {$id}", 'id desc' );
        return $next ? "<a href=\"{$this->archives_url($next)}\" title=\"{$next['title']}\">{$next['title']}</a>" : '没有了。';
    }

    public function add_comment( $c ) {

        if ( isset( $_POST[ 'verify' ] ) && strtolower( $_POST[ 'verify' ] ) != $_SESSION[ "verify" ] ) {
            exit( '验证码错误!' );
        }

        foreach ( $_POST as $k => $v ) {
            $rows[ $k ] = htmlspecialchars( trim( $v ) );
        }

        if ( !intval( $rows[ 'aid' ] ) ) {
            exit( '非法提交评论!' );
        }

        $rows[ 'dateline' ] = time();
        $rows[ 'ip' ] = S( 'ip' );
        $rows[ 'status' ] = $this->setting[ 'is_comment_audit' ] ? 0 : 1;

        $this->model->table = $c;
        $this->model->create( $rows ) ? 'success' : 'failed';

        $this->model->table = 'article';
        $this->model->incrField( array( 'id' => $rows[ 'aid' ] ), 'comments' );

        exit( 'success' );
    }
}